package com.ganbo.stock.service.impl;

import com.ganbo.stock.entity.Company;
import com.ganbo.stock.entity.Idea;
import com.ganbo.stock.entity.Stock;
import com.ganbo.stock.enums.ServiceType;
import com.ganbo.stock.utils.StringUtils;
import com.ganbo.stock.repository.CompanyRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公司简介
 * Created by gan on 2020/12/26 20:47.
 */

@Service
public class CompanyService extends BaseService {

    @Autowired
    StockService stockService;
    @Autowired
    CompanyRepository companyRepository;
    @Autowired
    IdeaService ideaService;


    public List<Company> findAll() {
        return companyRepository.findAll();
    }


    public Company getUpdateCompanyByVersion() {
        Page<Company> page = companyRepository.findMinVersion(PageRequest.of(0, 1));
        List<Company> companies = page.getContent();
        if (!CollectionUtils.isEmpty(companies)) return companies.get(0);
        return null;
    }


    public void updateCompany() {
        Stock stock = stockService.getUpdateCompanyStock();
        if (stock == null) return;

        Integer updateCompanyVersion = stock.getUpdateCompanyVersion() == null ? 1 : stock.getUpdateCompanyVersion() + 1;
        stock.setUpdateCompanyVersion(updateCompanyVersion);
        stockService.save(stock);

        Date now = new Date();
        Company companyDB = companyRepository.findByDm(stock.getDm());
        Company company = getByDm(stock.getDm());
        if (companyDB == null) {
            companyDB = new Company();
        }

        Integer version = company.getVersion() == null ? 1 : company.getVersion() + 1;
        Long id = companyDB.getId();
        companyDB.setStockId(stock.getId());
        BeanUtils.copyProperties(company, companyDB);
        companyDB.setStockName(stock.getMc());
        companyDB.setId(id);
        companyDB.setDm(stock.getDm());
        companyDB.setCreateTime(now);
        companyDB.setStockId(stock.getId());
        companyDB.setVersion(version);
        companyRepository.save(companyDB);

        //将该股票的板块信息加入版块表中
        String ideaNames = company.getIdea();
        if (StringUtils.isBlank(ideaNames)) return;
        String[] names = ideaNames.split(",");
        for (String name : names) {
            Idea idea = ideaService.findIdeaByName(name);
            if (idea != null) continue;
            idea = new Idea();
            idea.setName(name);
            ideaService.save(idea);
        }
    }


    public Company getByDm(String dm) {
        Map<String, String> params = new HashMap<>();
        params.put("dm", dm);
        params.put("licence", ig507Config.getLicence());
        return sendReq(ServiceType.COMPANY_INFO, params, Company.class);
    }

}
